/**
 * 玩家死亡
 */
class playerDied extends eui.Component {
	public constructor(labelHint: eui.Label, countDownLabel: eui.Label) {
		super();
		this.wenzi = labelHint;
		this.countDownTime = countDownLabel;
	}
	/**文字文本 */
	public wenzi: eui.Label;
	/**5秒计时 */
	private countDownTime: eui.Label;
	/***设置继续游戏事件 */
	private setGameAgainEvent() {
		SoundManager.getInstance().sound_effect_switch = true;
		SoundManager.getInstance().playEffectSound("click_mp3");
		this.updataCountRest();

		var useData = {
			"roomId": DataProl.roomid,
			"account": DataProl.account,
		};
		// //11111
		// webNetworkMananger.getInstance().gameSceneRec.isUserDied = false;
		if (webNetworkMananger.isServerMove) {
			let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
			let m_c2sRespawn = new awesomepackage.c2sRespawn();
			m_c2sRespawn.account = DataProl.account;

			m_c2sRespawn.sign = DataProl.sign;
			m_c2sRespawn.roomId = DataProl.roomid;
			m_AwesomeMessage.meesageId = 1006;
			m_AwesomeMessage.data = awesomepackage.c2sRespawn.encode(m_c2sRespawn).finish();

			var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
			var byte2: egret.ByteArray = new egret.ByteArray(buffer);
			webNetworkMananger.getInstance().SendBitMeesage(byte2);
		}

		else {
			webNetworkMananger.getInstance().gameSceneRec.isUserDied = false;
			AIServer.getInstance().respawnMessageManager(DataProl.account, DataProl.login_data.nickName);
		}
		//this.visible = false;

	}
	/**设置返回大厅事件 */
	private setBackToHallEvent() {
		DataProl.dei = 0;//一局内死亡次数清零
		webNetworkMananger.getInstance().gameSceneRec.backToHall();

		if (webNetworkMananger.isServerMove) {
			let m_AwesomeMessage = new awesomepackage.AwesomeMessage();
			let m_c2sBackToHall = new awesomepackage.c2sBackToHall();
			m_c2sBackToHall.account = DataProl.account;

			m_c2sBackToHall.sign = DataProl.sign;

			m_AwesomeMessage.meesageId = 1005;
			m_AwesomeMessage.data = awesomepackage.c2sBackToHall.encode(m_c2sBackToHall).finish();

			var buffer = awesomepackage.AwesomeMessage.encode(m_AwesomeMessage).finish();
			var byte2: egret.ByteArray = new egret.ByteArray(buffer);
			webNetworkMananger.getInstance().SendBitMeesage(byte2);
		}
		//	webNetworkMananger.getInstance().SendMeesage('backToHall', DataProl.account);
		console.log("玩家: " + DataProl.account + "  离开游戏..");
		//this.visible = false;
	}

	/**5秒倒计时 */
	private updataCountTime: egret.Timer = new egret.Timer(1000, -1);
	private TIMEBackToHall = 2;

	/**显示玩家死亡面板 */
	public disPlayerDiedPanl() {
		if (DataProl.killnumMax >= 10) {
			SoundManager.getInstance().playEffectSound("shutdown_mp3");
		}
		else {
			SoundManager.getInstance().playEffectSound("gameOver_mp3");
		}
		//重置杀人数
		DataProl.killnumMax = 0;
		SoundManager.getInstance().sound_effect_switch = false;
		DataProl.dei++;
		//console.log("死亡次数" + DataProl.dei);
		webNetworkMananger.getInstance().gameSceneRec.isUserDied = true;
		//this.visible = true;
		var position = this.getRandomPostion();
		Allvariate.getInstance().FrogPoint.x = position.x;
		Allvariate.getInstance().FrogPoint.y = position.y;
		webNetworkMananger.getInstance().gameSceneRec.showResurrectionBox();

		this.setDisCountTime();
	}
	private getRandomPostion() {
		let x = this.randomInRange(serverConfig.config.gameMapBorder + (GlobalData.GameStage.stageWidth / DataProl.gameSceneScal) / 2, serverConfig.config.gameWidth - serverConfig.config.gameMapBorder - (GlobalData.GameStage.stageWidth / DataProl.gameSceneScal) / 2);
		let y = this.randomInRange(serverConfig.config.gameMapBorder + (GlobalData.GameStage.stageHeight / DataProl.gameSceneScal) / 2, serverConfig.config.gameHeight - serverConfig.config.gameMapBorder - (GlobalData.GameStage.stageHeight / DataProl.gameSceneScal) / 2);
		return { x: x, y: y };
	}
	public randomInRange(from, to) {
		let tonum = Math.max(from, to);
		let fromnum = Math.min(from, to);
		let random = Math.floor(Math.random() * fromnum + (tonum - fromnum));
		return random;
	};

	/**设置倒计时 */
	private setDisCountTime() {
		this.countDispos();
		this.updataCountTime.addEventListener(egret.TimerEvent.TIMER, this.countDispos, this);
		this.updataCountTime.start();
	}

	/**倒计时处理 */
	private countDispos() {
		this.countDownTime.text = this.TIMEBackToHall.toString();
		if (this.TIMEBackToHall < 0) {
			this.updataCountRest();
			//this.setBackToHallEvent();
			this.setGameAgainEvent();
			webNetworkMananger.getInstance().gameSceneRec.closeResurrectionBox();
			// GameOver.getInstance().gameGoOn();
			// this.visible = false;
			return;
		}
		this.TIMEBackToHall--;
	}

	private updataCountRest() {
		this.updataCountTime.removeEventListener(egret.TimerEvent.TIMER, this.countDispos, this);
		this.updataCountTime.reset();
		this.TIMEBackToHall = 2;
	}

	public GameOverDis() {
		//this.visible = false;
		this.updataCountRest();
	}
}